<?php

namespace Api\Member\AuthManager;

use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration as BaseMigration;

/**
 * 迁移
 */
class Migration extends BaseMigration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        /**
         * 管理员表
         */
        if (! Schema::hasTable('member__managers')) {
            Schema::create('member__managers', function (Blueprint $table) {
                $table->engine = 'InnoDB';
                $table->increments('id');

                $table->string('account')->unique()                      ->comment('账号');
                $table->string('password')->nullable()                   ->comment('登录密码');

                $table->string('country_code')                           ->comment('国际区号');
                $table->string('mobile')                                 ->comment('手机号');
                
                $table->ipAddress('last_ip')                             ->comment('最后登陆的 IP 地址');
                $table->unsignedTinyInteger('status')->default(1)        ->comment('状态：1、正常 2、禁用');
                $table->unsignedTinyInteger('auth_fail_num')->default(0) ->comment('身份校验错误次数');
                $table->timestamp('confine_time')->nullable()            ->comment('限制时间，若不为空，则此时间之前禁止登陆（间隔累加）');

                $table->index('account');
                $table->index('mobile');
                $table->timestamps();
                $table->softDeletes();
            });
        }

        /**
         * 管理员操作记录表
         */
        if (! Schema::hasTable('member__manager_logs')) {
            Schema::create('member__manager_logs', function (Blueprint $table) {
                $table->engine = 'InnoDB';
                $table->increments('id');

                $table->unsignedInteger('member_manager_id') ->comment('发起操作的管理员ID');
                $table->string('type_str')                   ->comment('操作类型字符串：由其他服务调用时指定');
                $table->string('description')                ->comment('对操作的详细描述：由其他服务调用时指定');
                $table->ipAddress('request_ip')              ->comment('发起请求的 IP 地址');

                $table->index('member_manager_id');
                $table->timestamps();
                $table->softDeletes();
            });
        }

    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('member__managers');
        Schema::dropIfExists('member__manager_logs');
    }

}
